package com.lmx.healthcode.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lmx.healthcode.R;
import com.lmx.healthcode.base.MyApp;

import java.text.SimpleDateFormat;
import java.util.Locale;


/**
 * @author Leung
 * @date 2020/9/9 20:33
 */
public class CommonUtil {

    public static String getNowTime(String regex) {
        long nowTime = System.currentTimeMillis();
        return formatDate(regex, nowTime);
    }

    public static String formatDate(String regex, long date) {
        return new SimpleDateFormat(regex, Locale.CHINESE).format(date);
    }

    public static String[] parseTime(String time) {
        return time.split("-");
    }

    public static TimePickerView showTimePicker(Context context) {
        TimePickerView timePickerView = new TimePickerBuilder(
                context,
                ((date, v) -> {
                    ((TextView) v).setText(CommonUtil.formatDate("HH:mm", date.getTime()));
                    v.setTag(date.getTime());
                })
        ).setType(new boolean[]{false, false, false, true, true, false}).isDialog(true).build();
        return timePickerView;
    }

    public static int isCollege(String college) {
        String[] colleges = MyApp.getInstance().getResources().getStringArray(R.array.colleges);
        for (int i = 0; i < colleges.length; i++) {
            if (college.equals(colleges[i])) {
                return i;
            }
        }
        return 0;
    }

    public static int isDormitory(String dormitory) {
        String[] dormitories = MyApp.getInstance().getResources().getStringArray(R.array.dormitories);
        for (int i = 0; i < dormitories.length; i++) {
            if (dormitory.equals(dormitories[i])) {
                return i;
            }
        }
        return 0;
    }

    public static int isType(String type) {
        if (!type.equals("住宿")) {
            return 1;
        }
        return 0;
    }

    public static int isOutSchoolDoor(String outSchoolDoor) {
        String[] doors = MyApp.getInstance().getResources().getStringArray(R.array.doors);
        for (int i = 0; i < doors.length; i++) {
            if (outSchoolDoor.equals(doors[i])) {
                return i;
            }
        }
        return 0;
    }

    public static int dp2px(int dp) {
        DisplayMetrics dm = MyApp.getInstance().getResources().getDisplayMetrics();
        return (int) (dp * dm.density);
    }

}
